#ifndef _APP_SERVER_H_
#define _APP_SERVER_H_

#include "serverinfo.h"

#include "net/net.h"
#include "pack/pack.h"
#include "proto/proto.h"

#include "iserver.h"

#include "serveraccepter.h"

namespace app {

class Server : public IServer, public net::ISocketHandler {
public:
    Server();
    ~Server();

public:
    IServerHandler* GetHandler();

private: /* Internal */
    void LoadConfigFile();
    void RegisterServer();
    void ConnectToServerManager();

    void OnRegisterServer(proto::RRegisterServer& packet);

private: /* IServer */
    virtual void Run();		
    virtual void Terminate(); 

    virtual void SetHandler(IServerHandler* handler);

    virtual uint32_t GetId();

    virtual uint32_t GetServerType();
    virtual void SetServerType(uint32_t server_type);

    virtual void AddWatcher(uint32_t server_type);

private: /* ISocketHandler */
    virtual void OnRead(net::ISocket* socket);
    virtual void OnWrite(net::ISocket* socket);

    virtual void OnClose(net::ISocket* socket);
    virtual void OnConnected(net::ISocket* socket);

    virtual void OnError(net::ISocket* socket, uint32_t error);

private:
    uint32_t sid;
    uint32_t server_type;

    IServerHandler* handler;
    net::IAccepter* accepter;
    net::ISocket* server_manager; /* 和ServerManager的连接 */
    ServerAccepter server_accepter;
};

}

#endif
